package cn.stu.framework.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * 文件实体类
 */
@Data
@ToString
@EqualsAndHashCode
@Document(indexName = "file")
public class File {

    /**
     * 主键ID
     */
    @NotNull(message = "文件ID不能为空", groups = {Update.class})
    @Id
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    /**
     * 文件名
     */
    @Field(name = "name", type = FieldType.Text)
    private String name;
    /**
     * 路径
     */
    @Field(name = "path", type = FieldType.Text)
    private String path;
    /**
     * 类型
     */
    @Field(name = "type", type = FieldType.Text)
    private String type;
    /**
     * 大小
     */
    @Field(name = "size", type = FieldType.Text)
    private String size;
    /**
     * 用户ID
     */
    @Field(name = "user_id", type = FieldType.Long)
    private Long userId;
    /**
     * 创建者id
     */
    @Field(name = "create_id", type = FieldType.Long)
    @TableField(fill = FieldFill.INSERT)
    private Long createId;


    /**
     * 父级ID
     */
    @NotNull(message = "父级目录ID不能为空", groups = {CreateFile.class, CreateFolder.class})
    @Field(name = "parent_id", type = FieldType.Long)
    private Long parentId;
    /**
     * 是否共享
     */
    @Field(name = "shared", type = FieldType.Boolean)
    private Boolean shared;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(name = "create_time", type = FieldType.Date, format = {}, pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(name = "update_time", type = FieldType.Date, format = {}, pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    /**
     * content-type
     */
    @Field(name = "content_type", type = FieldType.Text)
    private String contentType;

    public boolean isFolder() {
        return "文件夹".equals(this.type);
    }

    public interface CreateFile {
    }

    public interface CreateFolder {
    }

    public interface Update {
    }

}